<?php


namespace app\api\controller;

use think\Db;
class Cpkfs
{
    public function yi(){
        {
            //进行数据库查询nav
            $data = Db::name('cpkf')->alias('c')
                ->join('attachment b', 'c.thumb=b.id')
                ->field('c.id,c.name,c.desc,b.filepath')
                ->where('type',9)
                ->select();
            foreach ($data as $key => &$v) {
                $v['filepath'] = 'http://' . $_SERVER['HTTP_HOST'] . $v['filepath'];
            }
            if ($data) {
                return json(['code' => 200, 'msg' => "成功", 'data' => $data]);
            } else {
                return json(['code' => 0, 'msg' => "失败", 'data' => []]);
            }
        }
    }
    public function cp(){
        {
            $id = input('pid');
            if(!$id){
                return 0;
            }//判断有无参数
            //$id =  $this->requert->has('id');
            //进行数据库查询nav
            $data = Db::name('cpkf c')
                ->join('attachment b', 'c.thumb=b.id')
                ->field('c.id,c.name,c.desc,b.filepath')
                ->where('type',1)
                ->where('pid',$id)
                ->select();
            foreach ($data as $key => &$v) {
                $v['filepath'] = 'http://' . $_SERVER['HTTP_HOST'] . $v['filepath'];
            }
            if ($data) {
                return json(['code' => 200, 'msg' => "成功", 'data' => $data]);
            } else {
                return json(['code' => 0, 'msg' => "失败", 'data' => []]);
            }
        }
    }

    public function Cpkfs(){
        {
            $id = input('pid');
            $cpkf = Db::name('cpkf')
                ->field('id,name,tag,pid,desc')
                ->where('type',2)
                ->where('pid',$id)
                ->select();
            $pid   = array_column($cpkf, 'id');
            $cpkf1 = Db::name('cpkf a')
                ->join('attachment b', 'a.thumb=b.id')
                ->field('a.pid,name,a.desc,b.filepath')
                ->where(['a.pid'=>['in',$pid]])
                ->select();

            $arr_pro  = [];
            foreach ($cpkf1 as &$v) {
                $v['filepath'] = 'http://' . $_SERVER['HTTP_HOST'] . $v['filepath'];
                $arr_pro[$v['pid']][] = $v;
            }
            foreach ($cpkf as $key => &$v) {
                $v['article'] = $arr_pro[$v['id']] ? $arr_pro[$v['id']] : '';//拼接数组
            }
            if (!$cpkf) {
                return json(['code' => 0, 'msg' => "无数据", 'data' => []]);
            }
            return json(['code' => 200, 'msg' => "成功", 'data' => $cpkf]);
        }
    }

    public function Cpkff(){
        {
            $id = input('pid');
            $cpkf = Db::name('cpkf')
                ->field('id,name,tag,pid,desc')
                ->where('type',3)
                ->where('pid',$id)
                ->select();
            $pid   = array_column($cpkf, 'id');
            $cpkf1 = Db::name('cpkf a')
                ->join('attachment b', 'a.thumb=b.id')
                ->field('a.pid,name,a.desc,b.filepath')
                ->where(['a.pid'=>['in',$pid]])
                ->select();

            $arr_pro  = [];
            foreach ($cpkf1 as &$v) {
                $v['filepath'] = 'http://' . $_SERVER['HTTP_HOST'] . $v['filepath'];
                $arr_pro[$v['pid']][] = $v;
            }
            foreach ($cpkf as $key => &$v) {
                $v['article'] = $arr_pro[$v['id']] ? $arr_pro[$v['id']] : '';//拼接数组
            }
            if (!$cpkf) {
                return json(['code' => 0, 'msg' => "无数据", 'data' => []]);
            }
            return json(['code' => 200, 'msg' => "成功", 'data' => $cpkf]);
        }
    }
}